<template>
  <div class="upload-cover" @click="showCoverSelect">
    <div class="cover-wrap">
      <img class="cover-image" ref="cover-image" :src="value">
    </div>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      append-to-body
      @closed="dialogClosed">
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="素材库" name="first">
          <image-list :upload-image="false" :show-action="false" :page-size="18" :show-selected="true" ref="image-list"></image-list>
        </el-tab-pane>
        <el-tab-pane label="上传图片" name="second" class="upload-pane">
          <label for="file" class="upload-cover">
            <img ref="preivew-image" class="preivew-image">
          </label>
          <input type="file" @change="onFileChange" ref="file" id="file" hidden>
        </el-tab-pane>
      </el-tabs>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onCoverConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import ImageList from '@/views/image/components/image-list.vue'
import { uploadImage } from '@/api/image.js'
export default {
  name: 'UploadCover',
  components: {
    ImageList
  },
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      dialogVisible: false,
      activeName: 'first'
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    showCoverSelect () {
      this.dialogVisible = true
    },
    onFileChange () {
      const file = this.$refs.file.files[0]
      const blob = window.URL.createObjectURL(file)
      this.$refs['preivew-image'].src = blob
      // this.$refs.file.value = ''
    },
    onCoverConfirm () {
      if (this.activeName === 'second') {
        this.onUploadCover()
      } else if (this.activeName === 'first') {
        const imageList = this.$refs['image-list']
        const selected = imageList.selected
        if (selected === null) {
          this.$message({
            message: '请选择上传的封面',
            type: 'warning'
          })
          return false
        }
        this.$emit('input', imageList.images[selected].url)
      }
      this.dialogVisible = false
    },
    onUploadCover () {
      const file = this.$refs.file.files[0]
      if (!file) {
        this.$message({
          message: '请选择上传的封面',
          type: 'warning'
        })
        return false
      }
      const fn = new FormData()
      fn.append('image', file)
      uploadImage(fn).then(res => {
        this.$refs['cover-image'].src = res.data.data.url
        // this.$emit('update-cover', res.data.data.url)
        this.$emit('input', res.data.data.url)
      })
    },
    dialogClosed () {
      this.$refs.file.value = ''
    }
  }
}

</script>
<style lang='less' scoped>
.cover-wrap {
  width: 120px;
  height: 120px;
  // border: 1px solid #cccccc;
  border-radius: 5px;
  background: url("../img/select.png") center no-repeat;
  .cover-image {
    height: 120px;
    max-width: 100%;
  }
}
.upload-pane {
  display: flex;
  justify-content: center;
  align-items: center;
}
.upload-cover {
  display: block;
  width: 120px;
  height: 120px;
  border: 1px dashed #cccccc;
  border-radius: 5px;
  background: url("../img/plus.png") center no-repeat;
  .preivew-image {
    height: 120px;
    max-width: 100%;
  }
}
</style>
